const path = require('path');
const fs = require('fs');
const modules = require('../modules')

// https://github.com/jantimon/html-webpack-plugin
const HtmlWebpackPlugin = require('html-webpack-plugin');
const currentGitBranch = require('current-git-branch')

// Jenkins 目录
const jenkinsFrontCodeCachePath = '/root/front_code/cache/'
// 是否为 Jenkins 缓存 目录
const isJenkinsCache = fs.existsSync(jenkinsFrontCodeCachePath)

module.exports = {
  /** 生成sass-resources-loader */
  genSassResourceLoader(){
    return {
      loader: 'sass-resources-loader',
      options: {
        hoistUseStatements: true,
        resources: [
          path.resolve(__dirname, '../src/assets/scss/theme_color/index.scss'),
          path.resolve(__dirname, '../src/assets/scss/_themes.scss'),
          path.resolve(__dirname, '../src/assets/scss/_variables.scss'), 
          path.resolve(__dirname, '../src/assets/scss/_mixins.scss'),
        ]
      }
    }
  },
  
  /** 根据各个模块的配置生成entry */
  genEntry(modulesData) {
    const modulesObject = modulesData || modules
    let entry = {};
    for(let name in modulesObject){
      // 为支持热更新，必须为数组
      if(!Array.isArray(entry[name])) entry[name] = [];
      entry[name].push(modulesObject[name].entry) ;
    }
    return entry;
  },
  /** 根据各个模块的配置生成HtmlPlugin, 构建生产环境资源时用 */
  genHtmlPlugins(modulesData) {
    const modulesObject = modulesData || modules
    return Object.keys(modulesObject).map(modName => {
      let mod = modules[modName];
      let chunks = ['vendors', 'common', modName];
      
      if(mod.exclude) chunks = chunks.filter(item => mod.exclude.indexOf(item) < 0);
      
      let pageLoadingHtml = '<div id="loader-wrapper"><svg viewBox="25 25 50 50" class="circular"><circle cx="50" cy="50" r="20" fill="none" class="path"></circle></svg></div>'
      return new HtmlWebpackPlugin({
        filename: `${modName}.html`,
        template: mod.template ? mod.template : 'src/templates/index.html',
        inject: true,
        chunks,
        chunksSortMode: 'manual',
        isShowPageLoading:mod.isShowPageLoading ? pageLoadingHtml : '',
        minify: { // 压缩html
          collapseWhitespace: true,
          useShortDoctype: true
        }
      }); 
    })
  },
  /** 
   * @description 获取 webpack 缓存目录
  */
  getWebpackCacheDir() {
    
    // 是 Jenkins 缓存
    if (isJenkinsCache) {
      
      // 当前项目的父级目录
      const projectParentFullPath = path.resolve(__dirname, '../../')
      // 当前项目的父级目录名称
      const projectParentName = projectParentFullPath.split('/').pop()
      // 缓存目录
      const cachePath = jenkinsFrontCodeCachePath + projectParentName + '/' + currentGitBranch()
      
      return path.resolve(cachePath)
    }
    
    // 本地缓存
    return path.resolve(__dirname, '../node_modules/.cache')
    
  },
  /** 
   * @description 获取 babel 缓存目录
  */
  getBabelCacheDir() {
    
    // 是 Jenkins 缓存
    if (isJenkinsCache) {
      
      // 当前项目的父级目录
      const projectParentFullPath = path.resolve(__dirname, '../../')
      // 当前项目的父级目录名称
      const projectParentName = projectParentFullPath.split('/').pop()
      // 缓存目录
      const cachePath = jenkinsFrontCodeCachePath + projectParentName + '-babel/' + currentGitBranch()
      
      return path.resolve(cachePath)
    }
    
    // 本地缓存
    return path.resolve(__dirname, '../node_modules/.cache')
    
  }
}